import sys
import os
import jsonpath

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)

# 定义日志文件路径
LOG_PATH = os.path.join(BASE_PATH, "log")
if not os.path.exists(LOG_PATH):
    os.mkdir(LOG_PATH)


class BDP_Json_Utils():

    def __init__(self):
        pass


    def unicode_to_chinese(self,unicode:str):
        '''
        unicode编码转换成中文字符
        '''
        return "{}".format(unicode)


    def get_list(self, d, key):
        '''
        unicode编码转换成中文字符
        '''
        really_key = '$..{}'.format(key)
        # print(really_key)
        return jsonpath.jsonpath(d, '..%s')

    def get_fid_from_field_preview(self,title,dictpayload):
        '''
        从字段预览接口返回的res中获取到fid
        '''
        field_id = ""
        fields_list = dictpayload["result"]["fields"]
        for field in fields_list:
            if self.unicode_to_chinese(field["title"]) == title:
                field_id = field["field_id"]
        return field_id



bdp_json_utils = BDP_Json_Utils()
#
# if __name__ == '__main__':
#     json_utils = Json_Utils()
#     title = "NOW()_1647490296961"
#     test = {
#         "status": "0",
#         "errstr": "",
#         "result": {
#             "count": 1,
#             "status": 1,
#             "update_time": 1647490298,
#             "force_merge": 0,
#             "special_status": 0,
#             "update_mode": 0,
#             "fields": [{
#                 "remark": "",
#                 "original_name": "\u56fd\u5bb6\u540d\u79f0",
#                 "name": "\u56fd\u5bb6\u540d\u79f0",
#                 "title": "\u56fd\u5bb6\u540d\u79f0",
#                 "aggregator": "",
#                 "selected": True,
#                 "field_id": "fkaa509736",
#                 "real_type": None,
#                 "seq_no": 0,
#                 "uniq_index": 0,
#                 "fid": "fkaa509736",
#                 "type": 2
#             }, {
#                 "remark": "",
#                 "original_name": "NOW()_1647490296961",
#                 "name": "NOW()_1647490296961",
#                 "title": "NOW()_1647490296961",
#                 "is_modify": 1,
#                 "aggregator": "NOW()",
#                 "seq_no": 1,
#                 "field_id": "fka1a74ecd",
#                 "is_display": 0,
#                 "param": "",
#                 "display_aggr": "NOW()",
#                 "concrete": 0,
#                 "flag": 1,
#                 "tb_id": "tb_e16ff2d5875149bebb0e3b047abd58a9",
#                 "selected": True,
#                 "fid": "fka1a74ecd",
#                 "aggr": "NOW()",
#                 "owner": "6e5e73f1e063f2caeff2cf7df6405194",
#                 "raw_formula": "NOW()",
#                 "type": 3,
#                 "ctime": "2022-03-17 12:11:37"
#             }],
#             "partition": None,
#             "can_partition": False,
#             "comment": "",
#             "label": "",
#             "type": "excel",
#             "is_public_tb": 0,
#             "url_config": [],
#             "materialized": 1,
#             "has_gis": None,
#             "tb_type": "self",
#             "general": [],
#             "data": [
#                 ["\u4e2d\u56fd", "2022-03-17 12:11:54.427"]
#             ],
#             "vfield_partition": False,
#             "schema": [{
#                 "remark": "",
#                 "original_name": "\u56fd\u5bb6\u540d\u79f0",
#                 "name": "\u56fd\u5bb6\u540d\u79f0",
#                 "data_type": "string",
#                 "title": "\u56fd\u5bb6\u540d\u79f0",
#                 "aggregator": "",
#                 "field_id": "fkaa509736",
#                 "real_type": None,
#                 "seq_no": 0,
#                 "uniq_index": 0,
#                 "fid": "fkaa509736",
#                 "type": 2
#             }, {
#                 "remark": "",
#                 "original_name": "NOW()_1647490296961",
#                 "name": "NOW()_1647490296961",
#                 "data_type": "date",
#                 "title": "NOW()_1647490296961",
#                 "is_modify": 1,
#                 "aggregator": "NOW()",
#                 "seq_no": 1,
#                 "field_id": "fka1a74ecd",
#                 "is_display": 0,
#                 "param": "",
#                 "display_aggr": "NOW()",
#                 "concrete": 0,
#                 "flag": 1,
#                 "tb_id": "tb_e16ff2d5875149bebb0e3b047abd58a9",
#                 "fid": "fka1a74ecd",
#                 "aggr": "NOW()",
#                 "owner": "6e5e73f1e063f2caeff2cf7df6405194",
#                 "raw_formula": "NOW()",
#                 "type": 3,
#                 "ctime": "2022-03-17 12:11:37"
#             }]
#         },
#         "trcid": "31927ce8-f781-349c-afcd-ccd3baa61d31"
#     }
#     res = json_utils.get_fid_from_field_preview(title,test)
#     print(res)